/*
 * @LastEditors: yuSeen
 * @LastEditTime: 2021-11-21 14:08:14
 * 天凉好个秋
 */
import { Message } from "element-ui";
import { error } from "jquery";

// 滚动条滚动动画
function scrollTop(dom, number = 0, time) {
  if (!time) {
    dom.scrollTop = number;
    return number;
  }
  const spacingTime = 20; // 设置循环的间隔时间  值越小消耗性能越高
  let spacingInex = time / spacingTime; // 计算循环的次数
  let nowTop = dom.scrollTop; // 获取当前滚动条位置
  let everTop = (number - nowTop) / spacingInex; // 计算每次滑动的距离
  let scrollTimer = setInterval(() => {
    if (spacingInex > 0) {
      spacingInex--;
      this.scrollTop(dom, (nowTop += everTop));
    } else {
      clearInterval(scrollTimer); // 清除计时器
    }
  }, spacingTime);
}

//时间格式化
function timeformat(time) {
  let re = { day: "", hour: "", minute: "", second: "" };
  re.day = Math.floor(time / 1000 / 60 / 60 / 24);
  re.hour = Math.floor((time / 1000 / 60 / 60) % 24)
    .toString()
    .padStart(2, "0");
  re.minute = Math.floor((time / 1000 / 60) % 60)
    .toString()
    .padStart(2, "0");
  re.second = Math.floor((time / 1000) % 60)
    .toString()
    .padStart(2, "0");
  return re;
}

function icon(img) {
  return require("@/assets/" + img);
}

//打开对应模块属性项并关闭其他项
function openAttr(arr, index) {
  if (arr[index] == undefined) return;
  console.log(55, index);
  for (let i of arr) {
    i.open = false;
  }
  arr[index].open = true;
}

//将数组内的某项向上移动(向上交换位置)
function itemUp(list, index) {
	list[index] = list.splice(index - 1, 1, list[index])[0]
}

//将数组内的某项向下移动(向下交换位置)
function itemDown(list, index) {
	list[index] = list.splice(index + 1, 1, list[index])[0]
}

//删除数组内某项
function itemDel(arr, index) {
  arr.splice(index, 1);
}

//添加模块项
function addItem(arr, item) {
  arr.push(...item);
}

//提示
function tips(text, type = "success", time = 1500) {
  Message({ type, message: text, center: true, duration: time });
}

// 判断是否是空对象
function emptyObj(obj) {
  if (typeof obj != "object") return true;
  return Object.keys(obj).length ? false : true;
}

// 生成随机字符串，用来做key
function randomString(len = 10) {
  var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
    a = t.length,
    n = "";
  for (let i = 0; i < len; i++) n += t.charAt(Math.floor(Math.random() * a));
  return n;
}

export default {
  scrollTop,
  icon,
  timeformat,
  openAttr,
  tips,
  addItem,
  emptyObj,
  itemUp,
  itemDown,
  itemDel,
  randomString
};
